package util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.lang.reflect.Method;

import app.MyApplication;

/**
 * Created by Administrator on 2016/6/22.
 * 判断网络是否可用
 */
public class NetUtils {

    private static NetUtils mNetUtils;
    private Context mContext;
    private NetUtils(Context context){
            this.mContext = context.getApplicationContext();
    }

    public static NetUtils getInstance(Context context){
        if(mNetUtils == null){
            synchronized (NetUtils.class){
                if(mNetUtils == null){
                    mNetUtils = new NetUtils(context);
                            return mNetUtils;
                }
            }
        }
        return mNetUtils;
    }

    public boolean checkNetWork(){
        Context context = mContext.getApplicationContext();
        // 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager == null)
        {
            return false;
        }
        else
        {
            // 获取NetworkInfo对象
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

            if (networkInfo != null && networkInfo.length > 0)
            {
                for (int i = 0; i < networkInfo.length; i++)
                {
                    System.out.println(i + "===状态===" + networkInfo[i].getState());
                    System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
                    // 判断当前网络状态是否为连接状态
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 判断网络类型
     * @return 1 代表wifl 0移动网络 3 错误
     */
    public int getNetWorkTeyt(){
        ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if(info != null && info.isAvailable()){
            if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                return 1;
                //WiFi网络
            } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                    if(MyApplication.getApp().getSiteAreaid() == 0){
                        return 0; //移动网络
                    }else {
                        return 1;
                    }
            } else {
                //网络错误
                return 3;
            }
        }else{
            //网络错误
            return 3;
        }
    }

    /**
     * 判断网络类型
     * @return 1 代表wifl 0移动网络 3 错误 6  移动数据和WIFi 都开了的情况
     */
    public int getNetWorkTeyt1(){
        ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if(info != null && info.isAvailable()){
            if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                LogUtil.eE("NetWorkTeyt","..........1..........");
                return 1;
                //WiFi网络
            } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                if(MyApplication.getApp().getSiteAreaid() == 0){
                    return 0; //移动网络
                }else {
                    LogUtil.eE("NetWorkTeyt","..........2..........");
                    return 6;
                }
            } else {
                //网络错误
                return 3;
            }
        }else{
            //网络错误
            return 3;
        }
    }
}
